﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Threading.Tasks;


namespace WZ.DC.Config.Configuration
{
    public class DCConfiguration : ConfigurationSection
    {
        #region Constants and Fields

        private const string ConfigSectionName = "DCDBConnectionConifg";

        #endregion

        #region Public Properties
        [ConfigurationProperty("DefaultDataBase")]
        public Database DefaultDataBase
        {
            get
            {
                return (Database)this["DefaultDataBase"] ?? new Database();
            }
        }
        [ConfigurationProperty("DBCollection")]
        public DBCollection CollectionConfig
        {
            get { return (DBCollection)this["DBCollection"] ?? new DBCollection(); }
        }
        #endregion
        public static DCConfiguracionBase GetConfig()
        {
            var fileConfig = (DCConfiguration)ConfigurationManager.GetSection(ConfigSectionName);
            var config = new DCConfiguracionBase
            {
                DefaultDataBase = fileConfig.DefaultDataBase,
                DataBaseList = new List<DataBaseConfigurationElement>()
            };
            foreach (CollectionElement item in fileConfig.CollectionConfig)
            {
                config.DataBaseList.Add(
                    new DataBaseConfigurationElement()
                    {
                        Name = item.Name,
                        DataBaseElement = item.Database
                    });
            }
            return config;
        }
    }
    public class Database : ConfigurationElement
    {
        #region Public Properties


        [ConfigurationProperty("type", IsKey = false, IsRequired = true)]
        public string Type
        {
            get { return this["type"] as string; }
        }
        [ConfigurationProperty("encrypt", IsKey = false, IsRequired = true)]
        public bool encrypt
        {
            get { return (bool)this["encrypt"]; }
        }
        [ConfigurationProperty("connStr", IsKey = false, IsRequired = true)]
        public string ConnStr
        {
            get { return this["connStr"] as string; }
        }
        #endregion
    }
    public class DBCollection : ConfigurationElementCollection
    {
        #region Public Properties

        [ConfigurationProperty("DBCollection", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(CollectionElement), AddItemName = "add", ClearItemsName = "clear",
            RemoveItemName = "remove")]
        public CollectionElement Collections
        {
            get { return (CollectionElement)base["DBCollection"]; }
        }

        #endregion

        #region Methods

        protected override ConfigurationElement CreateNewElement()
        {
            return new CollectionElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((CollectionElement)element).Name;
        }

        #endregion
    }
    public class CollectionElement : ConfigurationElement
    {
        #region Public Properties


        [ConfigurationProperty("dataBase", IsRequired = false)]
        public Database Database
        {
            get { return this["dataBase"] as Database; }
        }

        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return this["name"] as string; }
        }
        #endregion
    }

    public class DCConfiguracionBase
    {
        public Database DefaultDataBase { get; set; }
        public List<DataBaseConfigurationElement> DataBaseList { get; set; }
    }
    public class DataBaseConfigurationElement
    {
        public string Name { get; set; }
        public Database DataBaseElement { get; set; }
    }

}